/*
 * Powered By [leyvi]
 * Since 2014 - 2015
 */

var collectModel = require('../model/Collect');
var db = require('../../utils/db/mysqlDb');
var common = require('../../utils/commonUtils');
var Promise = require('promise');
var collectDb = {

};

var defaultSelectColumn = ' id,article_id,member_id,status,create_time,update_time,bak1,bak2,bak3,bak4,bak5';

var defaultTable = 'collect';
//getById
collectDb.getById = function(id){
    return new Promise(function(fulfill, reject){
        var sql = 'select ' + defaultSelectColumn + ' from ' + defaultTable+ ' where 1=1 ';
        if(typeof id === "object"){
            sql+='and id= ' + db.escape(id.id);
        }else{
            sql+=' and id= ' + db.escape(id);
        }
        db.query(sql).then(function(rows){
            if(rows[0]){
                fulfill(collectModel.dbConvertModel(rows[0]));
            }else{
                fulfill(null);
            }             
        }, function(err){
            reject(err);
        });
    });
};

//save
collectDb.save = function(obj, connection){
    return db.save('insert into ' + defaultTable+ '  set ?',
             collectModel.modelConvertDb(obj), connection);
};

//update
collectDb.update = function(obj, connection){
    var sql = 'update ' + defaultTable+ '  set ';
    if(common.isNotBlank(obj.articleId)){
        sql+='article_id=:articleId,';
    }
    if(common.isNotBlank(obj.memberId)){
        sql+='member_id=:memberId,';
    }
    if(common.isNotBlank(obj.status)){
        sql+='status=:status,';
    }
    if(common.isNotBlank(obj.createTime)){
        sql+='create_time=:createTime,';
    }
    if(common.isNotBlank(obj.updateTime)){
        sql+='update_time=:updateTime,';
    }
    if(common.isNotBlank(obj.bak1)){
        sql+='bak1=:bak1,';
    }
    if(common.isNotBlank(obj.bak2)){
        sql+='bak2=:bak2,';
    }
    if(common.isNotBlank(obj.bak3)){
        sql+='bak3=:bak3,';
    }
    if(typeof obj.bak4 != "undefined"){
        sql+='bak4=:bak4,';
    }
    if(typeof obj.bak5 != "undefined"){
        sql+='bak5=:bak5,';
    }
    sql+='where ';
        sql+='id=:id ';
        sql = sql.replace(',where', '  where');
    return db.update(sql, obj, connection);
};

//delete
collectDb.delete = function(id, connection){
    var sql = 'delete from ' + defaultTable+ '  where 1=1 ';
    if(typeof id === "object"){
        sql+=' and id= ' + db.escape(id.id);
    }else{
        sql+=' and id= ' + db.escape(id);
    }
    return db.update(sql, {}, connection);
};

collectDb.deleteByParam = function (params) {
    var sql = 'delete from ' + defaultTable+ '  where 1=1 ';
    sql+=' and article_id=:articleId';
    sql+=' and status=0';
    return db.update(sql, params);
}

//查询条件
var selectParams = function(params){
    var sql = '';
    if(params && typeof params.id != "undefined"){
        sql+=' and  id=' + db.escape(params.id);
    }
    if(params && typeof params.articleId != "undefined"){
        sql+=' and  article_id=' + db.escape(params.articleId);
    }
    if(params && typeof params.memberId != "undefined"){
        sql+=' and  member_id=' + db.escape(params.memberId);
    }
    if(params && typeof params.status != "undefined"){
        sql+=' and  status=' + db.escape(params.status);
    }
    if(params && typeof params.createTime != "undefined"){
        sql+=' and  create_time=' + db.escape(params.createTime);
    }
    if(params && typeof params.updateTime != "undefined"){
        sql+=' and  update_time=' + db.escape(params.updateTime);
    }
    if(params && typeof params.bak1 != "undefined"){
        sql+=' and  bak1=' + db.escape(params.bak1);
    }
    if(params && typeof params.bak2 != "undefined"){
        sql+=' and  bak2=' + db.escape(params.bak2);
    }
    if(params && typeof params.bak3 != "undefined"){
        sql+=' and  bak3=' + db.escape(params.bak3);
    }
    if(params && typeof params.bak4 != "undefined"){
        sql+=' and  bak4=' + db.escape(params.bak4);
    }
    if(params && typeof params.bak5 != "undefined"){
        sql+=' and  bak5=' + db.escape(params.bak5);
    }
    return sql;
};


//count
collectDb.count = function(params){
    return new Promise(function(fulfill, reject){
        var sql = 'select count(*) as total from ' + defaultTable;
        sql += ' where 1=1 ';
        sql += selectParams(params);
        db.query(sql).then(function(rows){
            fulfill(rows[0].total);
        }, function(err){
            reject(err);
        });
    });
};

//page
collectDb.getByPage = function(params, pageNo, pageSize, orderby){
    return new Promise(function(fulfill, reject){
        var sql = 'select ' + defaultSelectColumn + ' from ' + defaultTable;
        sql+=' where 1=1 ';
        sql += selectParams(params);
        sql+=' order by ' + orderby;
        if(pageNo <= 0){
            pageNo = 1;
        }
        pageNo = common.isNumber(pageNo) ? pageNo : 1;
        pageSize = common.isNumber(pageSize) ? pageSize : 10;
        sql += ' limit ';
        sql += (pageNo - 1) * pageSize;
        sql +=',';
        sql +=pageSize;
       
        db.query(sql).then(function(rows){
            var array = new Array();
            for(var p in rows){
                if(rows[p])array.push(collectModel.dbConvertModel(rows[p]));
            }
            fulfill(array);
        }, function(err){
            reject(err);
        });
    });
};

collectDb.getByParams = function(params, orderby){
    return new Promise(function(fulfill, reject){
        var sql = 'select ' + defaultSelectColumn + ' from ' + defaultTable;
        sql+=' where 1=1 ';
        sql += selectParams(params);
        sql+=' order by ' + orderby;
        db.query(sql).then(function(rows){
            var array = new Array();
            for(var p in rows){
                if(rows[p])array.push(collectModel.dbConvertModel(rows[p]));
            }
            fulfill(array);
        }, function(err){
            reject(err);
        });
    });
};

collectDb.db = db;
module.exports = collectDb;